/* Includes ------------------------------------------------------------------*/
#include "SystemTick.h"
#include "Led.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define LED_PRESCALE    100

/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
bool s_Led_IntFlag = false;
U8   s_Led_PrescaleCnt = 0;

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

void Led_TimerInterrupt(void)
{
    if (LED_PRESCALE <= ++s_Led_PrescaleCnt)
    {
        s_Led_PrescaleCnt = 0;
        s_Led_IntFlag = true;
    }
}


void Led_Init(void)
{
    // Initialize I/Os in Output Mode
    GPIO_Init(LED_PORT, LED_PINS, GPIO_MODE_OUT_PP_HIGH_SLOW);
    Led_SetLed(true);
    SysTick_Registration(&Led_TimerInterrupt);
}


void Led_SetLed(bool iOn)
{
    if (true == iOn)
    {
        GPIO_WriteHigh(LED_PORT, LED_PINS);
    }
    else
    {
        GPIO_WriteLow(LED_PORT, LED_PINS);
    }
}


void Led_InvLed(void)
{
    if ( RESET == GPIO_ReadInputPin(LED_PORT, LED_PINS) )
    {
        GPIO_WriteHigh(LED_PORT, LED_PINS);
    }
    else
    {
        GPIO_WriteLow(LED_PORT, LED_PINS);
    }
}


void Led_RunOne(void)
{
    static U8 s_Cnt = 0;
    if (true == s_Led_IntFlag)
    {
        s_Led_IntFlag = false;
        
        if (5 <= ++s_Cnt)
        {
            s_Cnt = 0;
            Led_InvLed();
        }
    }
}
